package com.sakura.mall.member.memberStatisticsInfo.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.memberStatisticsInfo.domain.entity.MemberStatisticsInfoEntity;
import com.sakura.mall.member.memberStatisticsInfo.domain.request.AddMemberStatisticsInfoRequest;
import com.sakura.mall.member.memberStatisticsInfo.domain.request.UpdateMemberStatisticsInfoRequest;
import com.sakura.mall.member.memberStatisticsInfo.domain.request.MemberStatisticsInfoPageRequest;
import com.sakura.mall.member.memberStatisticsInfo.domain.vo.MemberStatisticsInfoVo;
import com.sakura.mall.member.memberStatisticsInfo.mapper.MemberStatisticsInfoMapper;
import com.sakura.mall.member.memberStatisticsInfo.service.MemberStatisticsInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员统计信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberStatisticsInfoServiceImpl extends ServiceImpl<MemberStatisticsInfoMapper, MemberStatisticsInfoEntity> implements MemberStatisticsInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberStatisticsInfoRequest request) {
        // todo 其他校验逻辑
        MemberStatisticsInfoEntity memberStatisticsInfo = request.convert(MemberStatisticsInfoEntity.class);
        this.save(memberStatisticsInfo);
    }

    @Override
    public Page<MemberStatisticsInfoVo> queryPage(MemberStatisticsInfoPageRequest request) {
        IPage<MemberStatisticsInfoEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberStatisticsInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberStatisticsInfoVo> ret = qp.convert(entity -> entity.convert(MemberStatisticsInfoVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberStatisticsInfoRequest request) {
        MemberStatisticsInfoEntity entity = request.convert(MemberStatisticsInfoEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

